//
//  NSString+Base64.m
//  WHPersonalData
//
//  Created by mac on 2018/1/4.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "NSString+Base64.h"

@implementation NSString (Base64)

/*
 方法名:  转换为Base64编码
 @param  无
 @return NSString   字符串
 */
- (NSString *)base64EncodedString
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}

// base64解码
- (NSString*)base64Decode
{
    // 1.先把base64编码后的字符串转成二进制数据
    NSData* data = [[NSData alloc] initWithBase64EncodedString:self options:0];
    // 2.把data转成字符串
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

/*
 方法名:  将Base64编码还原
 @param  encoding
 @return NSString   字符串
 */

- (NSString *)base64DecodedString:(NSStringEncoding)encoding
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
    NSString*string =[[NSString alloc]initWithData:data encoding:encoding];
    return string;
}

/*
 方法名:  GBK编码
 @param  encodedString
 @return NSString   字符串
 */

+(NSString*)base64withGBKEendedString:(NSString*)encodedString
{
    NSStringEncoding gbkEncoding=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    //存在中文要进GBK编码不然在服务端出现乱码
    NSData*reqinfoData = [encodedString dataUsingEncoding: gbkEncoding];
    NSString*base64name = [reqinfoData base64EncodedStringWithOptions:0];
    return base64name;
}

/*
 方法名:  GBK反编码
 @param  无
 @return NSString   字符串
 */

-(NSString*)base64WithGBKDecodedString
{
    NSStringEncoding gbkEncoding=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
    NSString*string =[[NSString alloc]initWithData:data encoding:gbkEncoding];
    return string;
}

@end
